function ShaderManager(context){
	this.context = context;
	this.shadersRepo = new Array();
	
	this.shadersRepo.push(new Shader("gradient-vertex-shader"
	,"x-shader/x-vertex"
	,"attribute vec3 aVertexPosition;attribute vec4 aVertexColor;uniform mat4 uMVMatrix;uniform mat4 uPMatrix;varying vec4 vColor;void main(void) {gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.5);vColor = aVertexColor;}"));
	
	this.shadersRepo.push(new Shader("gradient-fragment-shader",
	"x-shader/x-fragment",
	"precision mediump float;varying vec4 vColor;void main(void) {gl_FragColor = vColor;}"));
	
	this.shaders = new Array();
	
	this.selectShader = function(name){
		for(var i=0;i<this.shadersRepo.length;i++){
			if(this.shadersRepo[i].name==name){
				this.shadersRepo[i].create();
				this.shaders.push(this.shadersRepo[i]);
				return;
			}
		}
	}
	
	this.getShader = function(name){
		// find shader
		var shader = null;
		for(var i=0;i<this.shaders.length;i++){
			if(this.shaders[i].name==name){
				shader = this.shaders[i];
				break;
			}
		}
		
		if(!shader || !shader.loaded || !shader.element)return null;

		var contextShader;
		if (shader.type == "x-shader/x-fragment") {
			contextShader = this.context.createShader(this.context.FRAGMENT_SHADER);
		} else if (shader.type == "x-shader/x-vertex") {
			contextShader = this.context.createShader(this.context.VERTEX_SHADER);
		} else {
			return null;
		}

		this.context.shaderSource(contextShader, shader.script);
		this.context.compileShader(contextShader);

		if (!this.context.getShaderParameter(contextShader, this.context.COMPILE_STATUS)) {
			alert(this.context.getShaderInfoLog(contextShader));
			return null;
		}
		return contextShader;
	}
}

function Shader(name,type,script){
	this.name = name;
	this.type = type;
	this.script = script;
	this.loaded = false;
	this.element = null;
	
	this.create = function(){
		this.element = document.getElementById(this.name);
		if(this.element){
			this.loaded=true;
			return;
		}
		
		var scriptObj = document.createElement('script');
		var att=document.createAttribute("id");
		att.value= this.name;
		scriptObj.setAttributeNode(att);
		
		att=document.createAttribute("type");
		att.value= this.type;
		scriptObj.setAttributeNode(att);
		scriptObj.innerHTML = this.script;
		
		document.body.appendChild(scriptObj);
		this.loaded=true;
		this.element = scriptObj;
	}
}